package com.example.speeddating.controller;

import com.example.speeddating.annotaion.LoginUser;
import com.example.speeddating.annotaion.TokenCheck;
import com.example.speeddating.common.BaseResponse;
import com.example.speeddating.common.RespGenerator;
import com.example.speeddating.entity.Photo;
import com.example.speeddating.entity.TokenEntity;
import com.example.speeddating.enums.ResponseCodeEnum;
import com.example.speeddating.exception.CommonException;
import com.example.speeddating.service.PhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;

/**
 *
 * @author tina
 * @since 2023-07-12 02:40:06
 */
@RestController
@RequestMapping("/photo")
public class PhotoController {

    @Autowired
    private PhotoService photoService;

    @TokenCheck(roles = "user")
    @PostMapping("/add")
    public BaseResponse addPhoto(@LoginUser TokenEntity tokenEntity, @RequestBody HashMap<String,Object> jsonString){
        if (jsonString.get("url")==null){
            throw new CommonException(ResponseCodeEnum.CODE_600);
        }
        String url = jsonString.get("url").toString();
        Photo photo = new Photo();
        photo.setUrl(url);
        photo.setUserId(tokenEntity.getId());
        photoService.insertPhoto(photo);
        return RespGenerator.returnOK(photo);
    }

    @TokenCheck(roles = "user")
    @DeleteMapping("/del/{id}")
    public BaseResponse delPhoto(@LoginUser TokenEntity tokenEntity, Long id){
        if (id==null){
            throw new CommonException(ResponseCodeEnum.CODE_600);
        }
        photoService.deletePhoto(id);
        return RespGenerator.returnOK("删除成功");
    }

}
